//
//  UIView+Frame.m
//  iOS-Categories (https://github.com/shaojiankui/iOS-Categories)
//
//  Created by Jakey on 14/12/15.
//  Copyright (c) 2014年 www.skyfox.org. All rights reserved.
//

#import "UIView+Frame.h"

@implementation UIView (Frame)
#pragma mark - Shortcuts for the coords

- (CGFloat)top
{
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)right
{
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight:(CGFloat)right
{
    CGRect frame = self.frame;
    frame.origin.x = right - self.frame.size.width;
    self.frame = frame;
}

- (CGFloat)bottom
{
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom:(CGFloat)bottom
{
    CGRect frame = self.frame;
    frame.origin.y = bottom - self.frame.size.height;
    self.frame = frame;
}

- (CGFloat)left
{
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

#pragma mark - Shortcuts for frame properties

- (CGPoint)origin {
    return self.frame.origin;
}

- (void)setOrigin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGSize)size {
    return self.frame.size;
}

- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}
#pragma mark - Shortcuts for positions

- (CGFloat)centerX {
    return self.center.x;
}

- (void)setCenterX:(CGFloat)centerX {
    self.center = CGPointMake(centerX, self.center.y);
}

- (CGFloat)centerY {
    return self.center.y;
}

- (void)setCenterY:(CGFloat)centerY {
    self.center = CGPointMake(self.center.x, centerY);
}

-(void)cornerRadius{
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = self.width/2;
}
-(void)toCABasicAnimation{
    CABasicAnimation* shake = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    shake.fromValue = [NSNumber numberWithFloat:-5];
    shake.toValue = [NSNumber numberWithFloat:5];
    shake.duration = 0.2;//执行时间
    shake.autoreverses = NO; //是否重复
    shake.repeatCount = 2;//次数
    [self.layer addAnimation:shake forKey:@"shakeAnimation"];
    
}


-(void)addBackgroundImage:(UIImage *)image isBlur:(BOOL)blur{
  
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    imageView.frame = self.bounds;
    imageView.tag = 9097;
    [self insertSubview:imageView atIndex:0];
    
    if(blur){
  
        UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc]initWithEffect:beffect];
        effectView.frame = imageView.bounds;
        [imageView addSubview:effectView];
    }
    
    
}
-(UIImageView *)tp_addLineBottomWithColor:(UIColor *)color{
    UIImageView *line = [[UIImageView alloc]initWithImage:[UIImage imageWithColor:color]];
    line.tag = 12930;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.6);
        make.bottom.mas_equalTo(0.0f);
        make.left.right.mas_equalTo(0.0f);
    }];
    return line;
}

-(UIImageView *)tp_addLineTopWithColor:(UIColor *)color{
    UIImageView *line = [[UIImageView alloc]initWithImage:[UIImage imageWithColor:color]];
    line.tag = 12932;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.6);
        make.top.mas_equalTo(0.0f);
        make.left.right.mas_equalTo(0.0f);
    }];
    return line;
}
-(UIImageView *)tp_addLineBRightWithColor:(UIColor *)color{
    UIImageView *line = [[UIImageView alloc]initWithImage:[UIImage imageWithColor:color]];
    line.tag = 12931;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.6);
        make.height.mas_equalTo(self);
        make.centerY.mas_equalTo(self);
        make.right.mas_equalTo(0.0f);
    }];
    return line;
}

- (void)removeAllSubViews {
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
}


static void *ANSubstitutedViewAssociationKey = &ANSubstitutedViewAssociationKey;


- (void)tp_startAnimating{
    
    [self tp_stopAnimating];
    
  
    UIActivityIndicatorView* loader = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self addSubview:loader];
    
    loader.center = self.center;
    objc_setAssociatedObject(self, ANSubstitutedViewAssociationKey, loader, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    [loader startAnimating];
}

- (void)tp_stopAnimating {
    
    UIActivityIndicatorView* loader = objc_getAssociatedObject(self, ANSubstitutedViewAssociationKey);
    if(loader)
        [loader stopAnimating];
    objc_setAssociatedObject(self, ANSubstitutedViewAssociationKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}




@end
